/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __EB_SPI_H__
#define __EB_SPI_H__

#include <kernel/device.h>
#include <kernel/driver.h>


extern struct bus_type spi_bus;
struct spi_driver;
struct spi_device;


struct spi_transfer {
    const void *tx_buf;
    const void *rx_buf;
    unsigned   len;
};


struct spi_master {
    void*   io_base;
    int (*transfer)(struct spi_device *spi, struct spi_transfer *xfer);
    void (*set_cs)(struct spi_device *spi, bool enable);
    void (*setup)(struct spi_device *spi);
};


struct spi_device {
    struct device  device;
    struct spi_master *master;
    uint32_t       speed;
    uint8_t        mode;
    uint8_t        bits;
    uint16_t       addr;
    const void     *platform_data;
    const void     *private_data;
};


struct spi_driver {
    struct driver   driver;
    int (*probe)(struct spi_device *dev);
    void (*remove)(struct spi_device *dev);
    struct match_id  *id_table;
};


#ifdef __cplusplus
extern "C" {
#endif


static inline  struct spi_device* to_spi_device(struct device *dev)
{
    return container_of(dev, struct spi_device, device);
}


static inline  struct spi_driver* to_spi_driver(struct driver *drv)
{
    return container_of(drv, struct spi_driver, driver);
}


static inline int spi_device_register(struct spi_device *dev)
{
    dev->device.bus = &spi_bus;
    return device_register(&dev->device);
}


static inline void spi_device_unregister(struct spi_device *dev)
{
    device_unregister(&dev->device);
}


static inline int spi_driver_register(struct spi_driver *drv)
{
    drv->driver.bus = &spi_bus;
    return driver_register(&drv->driver);
}


static inline void spi_driver_unregister(struct spi_driver *drv)
{
    driver_unregister(&drv->driver);
}


static inline const void* spi_get_platform_data(struct spi_device *dev)
{
    return dev->platform_data;
}


static inline void spi_set_platform_data(struct spi_device *dev, void *data)
{
    dev->platform_data = data;
}


static inline const void* spi_get_private_data(struct spi_device *dev)
{
    return dev->private_data;
}


static inline void spi_set_private_data(struct spi_device *dev, void *data)
{
    dev->private_data = data;
}


#ifdef __cplusplus
}
#endif


#endif //!__EB_SPI_H__
